package com.gainloop;

import java.util.ArrayList;

import org.alexd.jsonrpc.JSONRPCException;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;


import com.gainloop.Adapters.UserAdapter;
import com.gainloop.Api.Users;
import com.gainloop.Api.Entity.User;


public class People extends ActivityAbstract{
	
	private UserAdapter adapter;
	private Runnable viewPeople;
	private ProgressDialog progressDialog = null;
	private ArrayList<User> people =new ArrayList<User>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.people);
		
		initHeader();
		headerTitle.setText(R.string.top_rated);
		ListView mListView = (ListView) findViewById(R.id.rankList);
		 
		adapter = new UserAdapter(this, R.layout.user_item, new ArrayList<User>());
		mListView.setAdapter(adapter);
		viewPeople = new Runnable() {
			@Override
			public void run() {
				getPeople();
			}
		};
		Thread thread = new Thread(null, viewPeople, "MagentoBackground");

		progressDialog = ProgressDialog.show(People.this,getString(R.string.wait_msg),
				getString(R.string.ret_data_msg), true);
		thread.start();

		mListView.setTextFilterEnabled(true);
		
		//mListView.setOnClickListener(new PeopleItemListener(user, adapter);

			
	
		
	}
	private void getPeople() {
		ArrayList<User> topUsers;
		Users users=new Users();
		try {
			topUsers=users.getTopUsers(1);
		} catch (JSONRPCException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
		for (User user:topUsers){
			people.add(user);
		}

		runOnUiThread(returnRes);
	}
	
	private Runnable returnRes = new Runnable() {

		@Override
		public void run() {
			if (people != null && people.size() > 0) {
				adapter.notifyDataSetChanged();
				
				for (int i = 0; i < people.size(); i++){
					adapter.add(people.get(i));
				}
			}
			progressDialog.dismiss();
			adapter.notifyDataSetChanged();
		}
	};
	 
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main_menu, menu);
		
		MenuItem mi_profile = menu.findItem(R.id.menuProfile);
		mi_profile.setIntent(new Intent(this, Profile.class));
		
		MenuItem mi_newWish = menu.findItem(R.id.menuNewWish);
		mi_newWish.setIntent(new Intent(this, AddWish.class));
		
		MenuItem mi_search_gain = menu.findItem(R.id.menuSearch); 
		mi_search_gain.setIntent(new Intent(this, GainSearch.class));
		
		MenuItem mi_home_gain = menu.findItem(R.id.menuHome); 
		mi_home_gain.setIntent(new Intent(this, Home.class));
		
		menu.removeItem(R.id.menuPeople);
		return true;
	}
}


